using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class fundies_controls_comments_iprelationship : Controls.DataManagerIPRelationshipControl
{
    private int CurrentPage
    {
        get
        {
            if (ViewState["CurrentPage"] != null)
            {
                return (int)ViewState["CurrentPage"];
            }
            else
            {
                return 1;
            }
        }
        set
        {
            ViewState["CurrentPage"] = value;
        }
    }

    protected override void ItemChanged()
    {
        Refresh();
    }

    private void Refresh()
    {
        if (CurrentIPRelationship != null)
        {
            mviewRelationship.SetActiveView(viewRelationship);
            CurrentPage = 1;

            if (CurrentIPRelationship.User != null)
            {
                lblUser.Text = Helpers.StringHelper.SafeFormat(CurrentIPRelationship.User.Username);
                lblUser.ForeColor = System.Drawing.Color.Black;
            }
            else
            {
                lblUser.Text = "[n/a]";
                lblUser.ForeColor = System.Drawing.Color.Gray;
            }

            RefreshGrid();
            RefreshBanButton();
        }
        else
        {
            mviewRelationship.SetActiveView(viewError);
        }
    }

    private void RefreshGrid()
    {
        DAL.DataManagerPage<DAL.Comment> history = DAL.Comments.GetByIPRelationshipAndPage(CurrentIPRelationship, CurrentPage);
        rptPages.DataSource = Helpers.PageNumberHelper.GetPages(history.Pages, CurrentPage, 20);
        rptPages.DataBind();
        Comments1.SetDataSource(history.Items);
    }

    protected void ChangePage(object sender, CommandEventArgs e)
    {
        CurrentPage = Convert.ToInt32(e.CommandArgument);
        RefreshGrid();
    }

    private void RefreshBanButton()
    {
        if (CurrentIPRelationship.Banned)
        {
            btnBan.Text = "Unban User";
            btnAnnihilate.Text = "Un-Annihilate User";
        }
        else
        {
            btnBan.Text = "Ban User";
            btnAnnihilate.Text = "Annihilate User";
        }

        if (CurrentIPRelationship.User != null)
        {
            if (CurrentIPRelationship.User.HasPermissions(DAL.User.UserStatus.Moderator))
            {
                btnBan.Enabled = false;
                btnAnnihilate.Enabled = false;
            }
            else
            {
                btnBan.Enabled = true;
                btnAnnihilate.Enabled = true;
            }
        }
    }

    protected void btnBan_Click(object sender, EventArgs e)
    {
        if (CurrentIPRelationship.Banned)
        {
            DAL.IPRelationships.UnbanIP(CurrentIPRelationship);
        }
        else
        {
            DAL.IPRelationships.BanIP(CurrentIPRelationship);
        }
        lblMsg.Visible = true;
        RefreshBanButton();
    }
    protected void btnAnnihilate_Click(object sender, EventArgs e)
    {
        if (CurrentIPRelationship.Banned)
        {
            DAL.IPRelationships.UnannihilateByIPRelationship(CurrentIPRelationship);
        }
        else
        {
            DAL.IPRelationships.AnnihilateByIPRelationship(CurrentIPRelationship);
        }

        Refresh();
    }
}
